#!/bin/ksh 

RC=$(ps -ef|  grep "check_errpt.sh "| grep -v grep| wc -l)
if [ $RC -gt 2 ] ; then
echo exiting due to previous process is not finished
exit 0
fi

#RECIPIENT=German.Kudinov@aero.bombardier.com
#RECIPIENT=q01adm@aero.bombardier.com
RECIPIENT=mtl_it_erp_technology_group_system_support@aero.bombardier.com


LASTLOST=0
if [ -e /tmp/errpt.LASTTIME ] ; then
STIME=`cat /tmp/errpt.LASTTIME`
else
STIME=`date +%m%d0000%y`
LASTLOST=1
fi
ETIME=`date +%m%d%H%M%y`

RC=$(errpt -s $STIME -e $ETIME | grep -v -E "`cat /var/code/files/list_of_messages_to_ignore_in_errpt`" | wc -l)
#RC=$(errpt -s $STIME -e $ETIME | grep  -E "`cat /usr/local/bin/list_of_messages_to_ignore_in_errpt" | wc -l)

echo $ETIME > /tmp/errpt.LASTTIME

if [ $RC -gt 1 ] ; then
cp -p /var/adm/ras/errlog /var/adm/ras/errlog.bak

	if [ $LASTLOST -eq 0 ] ; then
		echo "CRITICAL `hostname -s`: error log has new entries to follow. Error review was done in date/time range - from $STIME  to $ETIME (format MonthDayHourMinYear) on `date `\n\nLast error message is \n`/usr/bin/errpt | head -2| tail -1`\n\nSee errlog copy as errpt -i /var/adm/ras/errlog.bak \nInclude Error Identifies to the filter list /usr/local/bin/list_of_messages_to_ignore_in_errpt for script check_errpt.sh if they are not significant" | mail -s "CRITICAL `hostname -s`: error log has new entries" $RECIPIENT
	else
		echo "CRITICAL `hostname -s`: error log has new entries to follow. Error review was done in date/time range - from Midnight(Last script verification log is lost) to $ETIME (format MonthDayHourMinYear) on `date `\n\nLast error message is \n`/usr/bin/errpt | head -2|tail -1`\n\nSee errlog copy as errpt -i /var/adm/ras/errlog.bak\nInclude Error Identifies to the filter list /usr/local/bin/list_of_messages_to_ignore_in_errpt for script check_errpt.sh if they are not significant" | mail -s "CRITICAL `hostname -s`: error log has new entries" $RECIPIENT
	fi
fi

